package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.center.pojo.MachineStatus;
import com.tbit.uqbike.client.pojo.BatteryModelDetail;
import com.tbit.uqbike.client.pojo.BmsBattery;
import com.tbit.uqbike.client.service.BatteryModelDetailService;
import com.tbit.uqbike.client.service.BatteryService;
import com.tbit.uqbike.client.service.BmsBatteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("batteryService")
public class BatteryServiceImpl implements BatteryService {
    @Autowired
    private BatteryModelDetailService batteryModelDetailService;
    @Autowired
    private BmsBatteryService bMSBatteryService;

    @Override
    public void setBatteryAndMileage(MachineStatus machineStatus) {
        /**判断是否有BMS,如果没有使用电压，如果没有电压使用默认*/
        if (machineStatus.getSoc() != null && machineStatus.getSoc() > 0 && machineStatus.getSoc() <= 100) {
            machineStatus.setSocPercent(machineStatus.getSoc());

            /**查询电量和续航里程的关系*/
            BmsBattery bmsBattery = bMSBatteryService.getByMachineId(machineStatus.getMachineId());
            if (bmsBattery != null) {
                machineStatus.setSurplusMileage((int) ((machineStatus.getSoc() / 100F) * bmsBattery.getMileage()));
            }
        } else {
            if (machineStatus.getBatteryEU() != null) {
                Float vol = machineStatus.getBatteryEU() / 1000F;
                /**兼容终端采集电压溢出问题*/
                if (vol < 20F) {
                    vol = vol + 65F;
                }

                /**计算剩余电量和续航里程*/
                BatteryModelDetail batteryModelDetail = batteryModelDetailService.getByMachineId(machineStatus.getMachineId(), vol);
                if (batteryModelDetail != null) {
                    machineStatus.setSocPercent(batteryModelDetail.getBatteryPer());
                    machineStatus.setSurplusMileage(batteryModelDetail.getMilage());
                }
            }
        }

        /**如果为空显示默认值*/
        if (machineStatus.getSocPercent() == null) {
            machineStatus.setSocPercent(-1);
        }
        if (machineStatus.getSurplusMileage() == null) {
            machineStatus.setSurplusMileage(-1);
        }

    }


}  